const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({
  data: {
    bizToken: '',
    redirectUrl: '',
    goFaceDone: false, // 已跳转至公证签做人脸
  },
  onLoad(e) {
    console.log('---middle onLoad', e)
    this.setData(
      {
        bizToken: e.bizToken,
        redirectUrl: decodeURIComponent(e.redirectUrl),
      },
     // this.getStatus(),
      this.goFaceAuth
    )
    console.log(this.data.redirectUrl)
    //this.goFaceAuth()
    //this.auth();
  },
  auth:function () {
    var that = this;
    var userid = wx.getStorageSync("userid");
    util.request(api.getsigningStatus, {userid:userid}).then(function (res) {
      if(res.data === "0"){
        wx.showModal({
          title: '即将进行人脸认证，请点击确定，并在弹出的新窗口点击允许',
          // content: '即将进行人脸认证，请点击确定',
          showCancel: false, // 不显示取消按钮
          success(res) {
            if (res.confirm) {
                that.goFaceAuth();
            } 
          }
        })
      }
    })
  },
  toindex:function () {
    var that = this;
    var userid = wx.getStorageSync("userid");
    util.request(api.getsigningStatus, {userid:userid}).then(function (res) {
      if(res.data === "1"){
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  goFaceAuth(e) {
    console.log("人脸认证")
    const self = this
    const { bizToken } = this.data
    wx.navigateToMiniProgram({
      appId: 'wx1cf2708c2de46337', // 公证签小程序APPID
      path: `/pages/face/index?bizToken=${bizToken}`, // 刷脸页面地址
      // envVersion: 'release',
      success(res) {
        self.setData({
          goFaceDone: true,
        })
      },
      fail(err){
        console.log(err)
      }
    })
    console.log(this.data.goFaceDone)
  },
  //从数据库获取状态判断跳转到首页
  // getStatus:function () {
  //   var userid = wx.getStorageSync("userid");
  //   //console.log(userid)
  //   util.request(api.getsigningStatus, {userid:userid}).then(function (res) {
  //     //console.log(res)
  //     if(res.data === "1"){
  //       wx.switchTab({
  //         url: '/pages/index/index',
  //       })
  //     }
  //     else {
  //       wx.showToast({
  //         title: '未认证成功！', 
  //         icon: 'none', 
  //         duration: 1500, 
  //       })
  //     }
  //   })  
  // },

  authentication:function () {
    if (this.data.goFaceDone) {
      console.log(this.data.redirectUrl)
      wx.navigateTo({
        url: '/pages/certificasigning/index?authurl=' +encodeURIComponent(this.data.redirectUrl) ,
      })
    }
  },
  onShow() {
    this.toindex();
    console.log(this.data.redirectUrl),
    console.log('---middle onShow')
    const { goFaceDone, redirectUrl } = this.data
    // 防止从实名/意愿页进入后直接返回
    if (!goFaceDone) return
    this.authentication();
    // 如果已经跳转过，重置
    this.setData({
      goFaceDone: false,
    })
  
    let options
    let pages = getCurrentPages()
    // 早期版本使用 getCurrentPages().options 获取参数信息
    if (pages.length > 0) {
      options = pages[pages.length - 1].options
    } else {
      // 使用特定函数获取参数信息
      options = this.getEnterParamSync()
    }
    console.log('---options', options)
    console.log(decodeURIComponent(options.redirectUrl))
    // 从公证签小程序返回
    if (options.scene === 1038 && options.referrerInfo.extraData && options.referrerInfo.extraData.faceResult) {
      const pre = pages[pages.length - 2]
      // 重新加载实名页面
      if (pre.reloadPage && typeof pre.reloadPage === 'function') {
        pre.reloadPage(redirectUrl + `&timeStamp=${new Date().getTime()}`)
        wx.navigateBack({
          delta: 1,
        })
      }
    }

  },
  
  // 低版本兼容
  getEnterParamSync() {
    try {
      const pages = getCurrentPages()
      const currentPage = pages[pages.length - 1]
      const queries = currentPage.options || {}
      const eventChannel = currentPage.getOpenerEventChannel()
      return Object.assign({}, queries, eventChannel && eventChannel.data)
    } catch (e) {
      console.error('[Error][getEnterParamSync]', e)
      return {}
    }
  }
})
